// pages/message/index.js
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import { setTabBarIndex } from '../../utils/hooks';
const $api = require('../../request/api').API;
Page({
  data: {
    dataList: [],
    //分页相关
    pageNo: 1,
    totalPage: 1,
    userRole: 0
  },
  onShow() {
    setTabBarIndex(3);
    let userRole = wx.getStorageSync('user_role');
    if (userRole) {
      this.setData({
        userRole: userRole
      });
      this.messageList();
    }
  },
  onRefresh() {
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    wx.showLoading({
      title: '刷新中...'
    });
    this.messageList();

    setTimeout(() => {
      wx.showToast({
        title: '刷新成功'
      });
      wx.stopPullDownRefresh();
    }, 1000);
  },
  onPullDownRefresh() {
    //调用刷新时将执行的方法
    this.onRefresh();
  },
  onReachBottom() {
    const { pageNo, totalPage, dataList } = this.data;
    if (pageNo < totalPage) {
      $api.messageList({ pageNo: pageNo + 1, pageSize: 10 }).then(res => {
        const list = res.data.data.list;
        this.setData({
          dataList: dataList.concat(list),
          pageNo: pageNo + 1
        });
      });
    } else {
      Toast('没有更多啦');
    }
  },
  // 获取列表数据
  messageList() {
    $api.messageList({ pageNo: 1, pageSize: 10 }).then(res => {
      let data = res.data.data;
      this.setData({
        dataList: data.list,
        totalPage: data.totalPage
      });
    });
  },
  handleNavigationDetails(e) {
    const data = e.currentTarget.dataset.item;
    if (data) {
      wx.navigateTo({
        url: `/subcontract/new/pages/msgDetails/index?item=${encodeURIComponent(JSON.stringify(data))}`
      });
    }
  }
});
